<?php

namespace Cktadmin\Controller;

use Common\Controller\BasicAdmin;
use Common\Service\DataService;
use Common\Service\ToolsService;
use Common\Service\BaseAdmin;

class ColumnController extends BasicAdmin {

    public $table = 'column';

    public function index() {
        $this->title = '栏目管理';
        return parent::_list($this->table);
    }

    protected function _index_data_filter(&$data) {
        $data = get_column($data);
        foreach ($data as &$vo) {
            $vo['name'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;├ ', $vo['level']) . $vo['name'];
            $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
        }
    }

    public function add() {
        return parent::_form($this->table, 'add');
    }

    public function edit() {
        return parent::_form($this->table, 'edit');
    }

    protected function _form_filter() {
        if (IS_GET) {
            $cate = BaseAdmin::getColumn(0);
            $this->assign('cate', $cate);
        }
    }

    public function del() {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    public function forbid() {
        if (DataService::update($this->table)) {
            $this->success("分类禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    public function resume() {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

}
